from watchdog.observers import Observer
from watchdog.events import *
import logging
import time

# 路径
path = r"D:\test_wacthdog"

# 日志基础设置,日志等级info，格式时间加信息
logging.basicConfig(level = logging.INFO,format='%(asctime)s %(message)s',datefmt='%Y-%m-%d %H:%M:%S')

# 事件处理器
log_event_handler = LoggingEventHandler()

# 创建监控对象
obs = Observer()

# 指定监控对象，以及事件处理器
obs.schedule(log_event_handler,path,recursive=True)

# 启动线程
obs.start()

# 对KeyboardInterrupt异常进行处理
try:
    while True:
        time.sleep(1)
except KeyboardInterrupt:
    obs.stop()

# 等待所有线程结束，才结束主进程
obs.join()



